package cdc.cs.service;

import cdc.cs.common.HttpWithAuthService;
import cdc.cs.config.ODataConfig;
import cdc.cs.entity.BusinessPartnersAddressVO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.IOException;


/**
 * @ClassName: ODataPartnersAddressService
 * @Description: TODO
 * @Author: kevin
 * @Date: 2020/5/28 15:28
 * @Version: 1.0
 */
@Service
@Slf4j
public class ODataPartnersAddressService {
    private BusinessPartnersAddressVO addressVO;

    /**
     * 获取单个地址
     * @param url
     * @return
     */
    public String getOneAddress(String url) {
        String result = null;
        int count = 2;
        while (count > 0) {
            try {
                result = HttpWithAuthService.getService(url);
            } catch (IOException e) {
                log.error("获取Partner总数网络异常，异常信息为:{}，地址为:{}", e.getMessage(), url);
            }
            if (StringUtils.isEmpty(result)) {
                count--;
            } else {
                break;
            }

        }
        return result;
    }

    /**
     * 解析地址
     * @param response
     * @return
     */
    private BusinessPartnersAddressVO parseAddressJson(String response) {
        BusinessPartnersAddressVO addressVO = new BusinessPartnersAddressVO();
        JSONObject obj = (JSONObject) JSONObject.parse(response);
        JSONObject d = (JSONObject) obj.get("d");
        JSONArray results = d.getJSONArray("results");
        if (results.size() > 0) {
            for (int i = 0; i < results.size(); i++) {
                JSONObject object = results.getJSONObject(i);
                addressVO.setBusinessPartner(object.getString("BusinessPartner"));
                addressVO.setCountry(object.getString("Country"));
                addressVO.setLanguage(object.getString("Language"));
                addressVO.setStreetName(object.getString("StreetName"));
                addressVO.setCityName(object.getString("CityName"));
                addressVO.setRegion(object.getString("Region"));
                addressVO.setPostalCode(object.getString("PostalCode"));

                JSONObject mobile = (JSONObject) object.get("to_MobilePhoneNumber");
                JSONObject mobile1 = (JSONObject) mobile.get("__deferred");
                String mobileString = mobile1.getString("uri");
                addressVO.setToPhone(mobileString+"?$format=json");
            }
            this.addressVO = addressVO;
            return addressVO;
        } else {
            return null;
        }

    }

    /**
     * 数据转换
     * @param addressVO
     * @return
     */
    public JSONObject dataChange(BusinessPartnersAddressVO addressVO) {
        JSONObject result = new JSONObject();
        result.put("BusinessPartner", addressVO.getBusinessPartner());
        result.put("CityName", addressVO.getCityName());
        result.put("Country", addressVO.getCountry());
        result.put("Language", addressVO.getLanguage());
        result.put("PostalCode", addressVO.getPostalCode());
        result.put("StreetName", addressVO.getStreetName());
        result.put("ToPhone", addressVO.getToPhone());
        result.put("Region", addressVO.getRegion());
        return result;
    }

    /**
     * 保存地址
     * @param
     * @return
     */
    public boolean savePartnerAddress(String url) {
        BusinessPartnersAddressVO addressVO = queryOneAddress(url);
        if (addressVO == null) {
            return false;
        } else {
            boolean result = savePartnerAddress(addressVO);
            if (!result) {
                return false;
            }
            return true;
        }
    }

    public boolean savePartnerAddress(BusinessPartnersAddressVO  addressVO) {
        JSONObject jsonObject = dataChange(addressVO);
        int count = 2;
        boolean flag = false;
        while (count > 0) {
            try {
                HttpWithAuthService.postService(ODataConfig.targetPartnerAddress, jsonObject);
                flag = true;
            } catch (IOException e) {
                log.error("保存PartnerAddress出现异常，未保存数据为:{}，地址为:{}", jsonObject, ODataConfig.targetPartnerAddress);
            }
            if (flag) {
                break;
            } else {
                count--;
            }
        }
        return flag;
    }

    /**
     * 获取数据
     * @param url
     * @return
     */
    public BusinessPartnersAddressVO queryOneAddress(String url) {
        String oneAddress = getOneAddress(url);
        if (StringUtils.isEmpty(oneAddress)) {
            return null;
        } else {
            return parseAddressJson(oneAddress);
        }
    }

    /**
     * 删除地址
     * @param url
     */
    public void deletePartnerAddress(String url) {
        int count = 2;
        boolean flag = false;
        while (count > 0) {
            try {
                HttpWithAuthService.deleteService(url);
                flag = true;
            } catch (IOException e) {
                log.error("删除TargetPartnerAddress异常，异常信息为:{}，地址为:{}", e.getMessage(), url);
            }
            if (flag) {
                break;
            } else {
                count--;
            }
        }
    }
}
